package com.framework.boc.statistics.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.boc.response.CommonResponse;
import com.framework.boc.statistics.entity.Template;
import com.framework.boc.statistics.request.vo.PageCondition;
import com.framework.boc.statistics.response.vo.TemplateStructureVo;
import com.framework.boc.statistics.service.impl.TemplateServiceImpl;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.framework.boc.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 统计模板表 前端控制器
 * </p>
 *
 * @author Leil
 * @since 2020-12-23
 */
@RestController
@RequestMapping("/statistics/template")
public class TemplateController extends BaseController {

    @Autowired
    TemplateServiceImpl templateService;

    @PostMapping("/save")
    public CommonResponse save(@RequestBody Template template) {
        boolean bool = this.templateService.addOrUpdate(template);
        return bool ? CommonResponse.success("保存成功") : CommonResponse.error("保存失败");
    }

    @PostMapping("/page")
    public CommonResponse page(@RequestBody PageCondition<Template> condition) {
        Page<Template> page = new Page<>(condition.getCurrent(), condition.getSize());
        LambdaQueryWrapper<Template> wrapper = Wrappers.lambdaQuery(condition.getCondition());
        IPage<Template> res = this.templateService.page(page, wrapper);
        return CommonResponse.success(res);
    }

    @GetMapping("/list")
    public CommonResponse list() {
        List<Template> list = this.templateService.list();
        return CommonResponse.success(list);
    }

    @GetMapping("/get-template-structure")
    public CommonResponse getTemplateStructure(@RequestParam("templateId") Integer templateId) {
        TemplateStructureVo templateStructure = this.templateService.getTemplateStructure(templateId);
        return CommonResponse.success(templateStructure);
    }

    @GetMapping("/delete")
    public CommonResponse delete(@RequestParam("templateId") Integer templateId) {
        boolean bool = this.templateService.deleteTemplate(templateId);
        return bool ? CommonResponse.success("删除成功") : CommonResponse.error("删除失败");
    }

}

